<template>
	<view class="flex-col page">

		<view class="flex-col group mt-9">
			<view class="flex-row justify-between items-center section">
				<view class="flex-row items-center">
					<image class="image_2" src='../../static/icons/location.png' />
					<view class="flex-col items-start ml-17">
						<text class="font text">{{orders.receiverName}},{{orders.receiverPhone}}</text>
						<text class="font_2 text_2 mt-9">{{orders.receiverDistrict}},{{orders.receiverAddress}}</text>
					</view>
				</view>

				<u-icon name="arrow-right"></u-icon>
			</view>
			<view class="flex-row items-center section_2 mt-7">
				<image class="image_4" :src="orders.productMeta.imageUrl" />
				<view class="flex-col items-start flex-1 ml-17">
					<text class="font_2 text_3">{{orders.productName}}</text>
					<view class="flex-row" style="padding-top: 20rpx; padding-left: 20rpx;">
						<u-avatar size="20" :src="orders.buyer.meta.avatar"></u-avatar>
						<view style="font-size: 28rpx; color:#888;">
							{{orders.buyer.nickname}}{{orders.buyer.mobile}}
						</view>
					</view>
					<view class="flex-row">
						<text class="font text_5">{{orders.unitPrice}}</text> <text
							style="padding: 5rpx;">x{{orders.quantity}}</text>
					</view>

				</view>
			</view>
			<view class="flex-col section_3 mt-7">
				<view class="flex-row justify-between items-center">
					<text class="font_2 text_6">单号：{{orders.id}}</text>
					<text class="text_7">{{getOrderStatus(orders.status)}}</text>
				</view>
				<view class="flex-col group_2">
					<view class="flex-row justify-between items-center group_3">
						<text class="font_2 text_8">实付款</text>
						<text class="font_2 text_9">￥{{orders.totalPrice?orders.totalPrice.toFixed(2):'0.00'}}</text>
					</view>
					<view class="flex-row justify-between mt-19">
						<text class="font_2">订单备注</text>
						<text class="font_2 text_10">{{orders.remark||'未填写备注'}}</text>
					</view>
				</view>
				<view class="flex-col group_2">
					<view style="padding-bottom: 20rpx;">
						<text style="font-weight: bold;font-size: 28rpx;">图片凭证</text>
						<text style="font-size: 28rpx;">(最多上传6张图片)</text>
					</view>

					<u-upload :fileList="fileList1" @afterRead="afterRead" width="100px" height="100px"
						@delete="deletePic" name="1" multiple :maxCount="6"></u-upload>
				</view>
				<view class="flex-col group_2">
					<view>
						<text style="font-weight: bold;font-size: 28rpx;">退货原因</text>
					</view>
					<u-textarea v-model="applyData.remarkByBuyer" placeholder="在这里填写退货原因"
						customStyle="background-color: #f2f2f2"></u-textarea>
				</view>
				<view class="flex-row justify-center items-center " style="margin-top: 20rpx;">

				</view>
				<button size="mini" type="primary" :disabled="submited" @click="doApply"
					style="margin: 20px; font-size: 32rpx; background-color: #ff0101 ;color: #fff; border-radius: 30px;">提交申请</button>
			</view>
		</view>
		<u-toast ref="utoast"></u-toast>
	</view>
</template>

<script>
	import Utils from '../../common/Utils';
	export default {
		components: {},
		props: {},
		data() {
			return {
				orders: {},
				fileList1: [],
				submited: false,
				applyData: {},
			};
		},
		onLoad(options) {
			if (options.orderId) {
				this.getOrderDetails(options.orderId)
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'success',
						message: '上传中'
					})
				})
				// for (let i = 0; i < lists.length; i++) {
				// 	//const result = await this.uploadFilePromise(lists[i].url)
				// 	let item = this[`fileList${event.name}`][fileListLen]
				// 	this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
				// 		status: 'success',
				// 		message: '',
				// 		url: ""
				// 	}))
				// 	fileListLen++
				// }
			},

			doApply() {
				if (this.submited) {
					return;
				}
		
				this.submited = true;

				if (!this.applyData.remarkByBuyer || this.fileList1.length == 0) {		 
					uni.showModal({
						content: '请填写退货原因或者上传退货图片凭证',
						showCancel: false,
					})
					this.submited = false;
					return;
				}
				uni.showLoading({
					title: '正在提交申请'
				})
				this.$Utils.SubscribeMessage('Fs_NF9FYIXgsS5gtLycDeg6m--38mtI6m0BBaK-hLGo', () => {
					if (this.fileList1.length > 0) {
						console.log(this.fileList1)
						this.$get({
							url: `${Utils.ApiUrl}/files/signature`,
							data: {
								dir: 'images/'
							}
						}).then(async (aRes) => {
							let i = 0;
							for (let file of this.fileList1) {
								const result = await Utils.uploadFilePromise(file.url, aRes);
								this.$set(this.applyData, 'imageUrl' + i, result)
								i++;
							}

							this.doSave();

						})
					} else {
						this.doSave();
					}
				})

			},
			doSave() {
				console.log(this.applyData)

				this.$post({
					url: Utils.ApiUrl + '/afterSales',
					data: this.applyData,
				}).then((res) => {
					uni.setStorageSync("@afterorders_liangpin", res)
					uni.hideLoading()
					this.$refs.utoast.show({
						message: "售后申请成功",
						complete() {
							uni.redirectTo({
								url: '/pages-aftersales/applySuccess/applySuccess'
							})
						}
					})



				}).catch((err) => {

				}).finally(() => {
					this.submited = false;
				})
			},

			goBack() {
				uni.navigateBack()
			},
			getOrderBtn(action) {
				return Utils.getOrderBtn(action);
			},
			getOrderStatus(status) {
				return Utils.getOrderStatus(status);
			},
			getOrderDetails(id) {
				this.$get({
					url: `${this.$Utils.ApiUrl}/orders/${id}`,
					data: {

					}
				}).then(res => {
					console.log(res)
					this.orders = res;
					this.$set(this.applyData, 'orderId', this.orders.id)
				})
			}
		},
	};
</script>

<style scoped lang="scss">
	button {
		border-radius: 30rpx;
		padding: 0 20rpx;
	}

	button:nth-child(1) {
		background-color: #fff;
		color: #666;
	}

	button:nth-child(2) {
		background-color: green;
		color: #ffffff;
	}

	button:nth-child(3) {
		background-color: #ff0101;
		color: #ffffff;
	}

	.mt-9 {
		margin-top: 15.7rpx;
	}

	.mt-7 {
		margin-top: 12.21rpx;
	}

	.mt-19 {
		margin-top: 33.14rpx;
	}

	.ml-17 {
		margin-left: 29.65rpx;
	}

	.page {
		background-color: #f2f2f2;
		width: 100%;
		overflow-y: auto;
		overflow-x: hidden;
		height: 100%;

		.image {
			width: 100vw;
			height: 25.5814vw;
		}

		.group {
			padding: 0 17.44rpx;

			.section {
				padding: 47.09rpx 27.91rpx;
				background-color: #ffffff;
				border-radius: 17.44rpx;

				.image_2 {
					width: 83.72rpx;
					height: 83.72rpx;
				}

				.text {
					color: #333333;
					line-height: 29.11rpx;
					font-weight: bold;
				}

				.text_2 {
					color: #666655;
					font-size: 28rpx;
					word-wrap: break-word;
					width: 70vw;
				}

				.image_3 {
					width: 62.79rpx;
					height: 62.79rpx;
				}
			}

			.section_2 {
				padding: 27.91rpx;
				background-color: #ffffff;
				border-radius: 17.44rpx;

				.image_4 {
					border-radius: 8.72rpx;
					width: 139.53rpx;
					height: 139.53rpx;
				}

				.text_3 {
					color: #575757; 
				}

				.text_4 {
					margin-left: 6.98rpx;
					margin-top: 38.37rpx;
					color: #808080;
					line-height: 26.76rpx;
				}

				.text_5 {
					margin-left: 6.98rpx;
					margin-top: 13.95rpx;
					color: #ff0101;
					line-height: 23.83rpx;
				}
			}

			.font_2 {
				font-size: 28rpx;
				font-family: SourceHanSansCN;
				color: #555555;
			}

			.font {
				font-size: 31.4rpx;
				font-family: SourceHanSansCN;
			}

			.section_3 {
				padding: 31.4rpx 27.91rpx 136.05rpx 34.88rpx;
				background-color: #ffffff;
				border-radius: 12.24rpx;

				.text_6 {
					color: #000000;
					line-height: 22.24rpx;
				}

				.text_7 {
					margin-right: 10.47rpx;
					color: #ff0101;
					font-size: 31.4rpx;
					font-family: SourceHanSansCN;
					font-weight: 700;
					line-height: 29.79rpx;
				}

				.group_2 {
					margin-top: 34.88rpx;

					.group_3 {
						padding-left: 6.98rpx;

						.text_8 {
							line-height: 22.66rpx;
						}

						.text_9 {
							color: #ff0101;
							line-height: 18.54rpx;
						}
					}

					.text_10 {
						margin-right: 10.47rpx;
						color: #333333;
						line-height: 22.73rpx;
					}
				}

				.group_4 {
					margin-top: 254.65rpx;
					padding: 0 10.67rpx;

					.text-wrapper {
						padding: 20.93rpx 0;
						background-color: #ffffff;
						border-radius: 33.14rpx;
						width: 211.05rpx;
						height: 68.02rpx;
						border-left: solid 1.74rpx #8a8a8a;
						border-right: solid 1.74rpx #8a8a8a;
						border-top: solid 1.74rpx #8a8a8a;
						border-bottom: solid 1.74rpx #8a8a8a;

						.text_11 {
							color: #383838;
						}
					}

					.text-wrapper_2 {
						padding: 20.93rpx 0;
						background-color: #ff0101;
						border-radius: 33.14rpx;
						width: 209.3rpx;
						height: 66.28rpx;

						.text_12 {
							color: #ffffff;
							line-height: 22.6rpx;
						}
					}
				}
			}
		}
	}
</style>